package cn.zzq.aix.nativeinterface.javareflector;

import java.util.Map;
import java.util.Set;

import com.google.appinventor.components.runtime.ActivityResultListener;
import com.google.appinventor.components.runtime.Form;
import com.google.appinventor.components.runtime.OnClearListener;
import com.google.appinventor.components.runtime.OnCreateOptionsMenuListener;
import com.google.appinventor.components.runtime.OnDestroyListener;
import com.google.appinventor.components.runtime.OnNewIntentListener;
import com.google.appinventor.components.runtime.OnOptionsItemSelectedListener;
import com.google.appinventor.components.runtime.OnPauseListener;
import com.google.appinventor.components.runtime.OnResumeListener;
import com.google.appinventor.components.runtime.OnStopListener;
import com.google.appinventor.components.runtime.collect.Maps;
import com.google.appinventor.components.runtime.collect.Sets;
import com.google.appinventor.components.runtime.util.OnInitializeListener;
import com.google.appinventor.components.runtime.util.YailProcedure;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

public class ActivityListener
		implements OnStopListener, OnClearListener, OnNewIntentListener, OnResumeListener, OnPauseListener,
		OnDestroyListener, OnInitializeListener, OnCreateOptionsMenuListener, OnOptionsItemSelectedListener {
	public final Set<YailProcedure> onStopListeners = Sets.newHashSet();
	public final Set<YailProcedure> onClearListeners = Sets.newHashSet();
	public final Set<YailProcedure> onNewIntentListeners = Sets.newHashSet();
	public final Set<YailProcedure> onResumeListeners = Sets.newHashSet();
	public final Set<YailProcedure> onPauseListeners = Sets.newHashSet();
	public final Set<YailProcedure> onDestroyListeners = Sets.newHashSet();

	// AppInventor lifecycle: listeners for the Initialize Event
	public final Set<YailProcedure> onInitializeListeners = Sets.newHashSet();

	// Listeners for options menu.
	public final Set<YailProcedure> onCreateOptionsMenuListeners = Sets.newHashSet();
	public final Set<YailProcedure> onOptionsItemSelectedListeners = Sets.newHashSet();

	public final Map<YailProcedure, ActivityResultListener> activityResultMap = Maps.newHashMap();

	public ActivityListener(Form form) {

		if (form != null) {
			Logger.log("ActivityListener", "开始注册Activity的各种事件监听器");
			//当第一次启动Screen初始化时执行该回调
			form.registerForOnInitialize(this);
			//当ai伴侣刷新时调用onClear回调
			form.registerForOnClear(this);
			//当程序返回后台时,依次调用OnPause回调与OnStop回调
			form.registerForOnPause(this);
			form.registerForOnStop(this);
			//当程序返回前台时调用OnResume方法
			form.registerForOnResume(this);
			//当程序被销毁时回调该方法
			form.registerForOnDestroy(this);
			
			form.registerForOnNewIntent(this);
			form.registerForOnCreateOptionsMenu(this);
			form.registerForOnOptionsItemSelected(this);
			Logger.log("ActivityListener", "JavaReflector组件初始化完毕");
		}

	}

	@Override
	public void onStop() {
		Logger.log("ActivityListener", "onStop");
		for (YailProcedure onStopListener : onStopListeners) {
			onStopListener.call();
		}
	}

	@Override
	public void onClear() {
		//ai伴侣刷新时
		Logger.log("ActivityListener", "onClear");
		for (YailProcedure onClearListener : onClearListeners) {
			onClearListener.call();
		}
	}

	@Override
	public void onNewIntent(Intent intent) {
		Logger.log("ActivityListener", "onNewIntent");
		for (YailProcedure onNewIntentListener : onNewIntentListeners) {
			onNewIntentListener.call(intent);
		}
	}

	@Override
	public void onResume() {
		Logger.log("ActivityListener", "onResume");
		for (YailProcedure onResumeListener : onResumeListeners) {
			onResumeListener.call();
		}
	}

	@Override
	public void onPause() {
		Logger.log("ActivityListener", "onPause");
		for (YailProcedure onPauseListener : onPauseListeners) {
			onPauseListener.call();
		}
	}

	@Override
	public void onDestroy() {
		Logger.log("ActivityListener", "onDestroy");
		for (YailProcedure onDestroyListener : onDestroyListeners) {
			onDestroyListener.call();
		}
	}

	@Override
	public void onInitialize() {
		Logger.log("ActivityListener", "onInitialize");
		for (YailProcedure onInitializeListener : onInitializeListeners) {
			onInitializeListener.call();
		}
	}

	@Override
	public void onCreateOptionsMenu(Menu menu) {
		Logger.log("ActivityListener", "onCreateOptionsMenu");
		for (YailProcedure onCreateOptionsMenuListener : onCreateOptionsMenuListeners) {
			onCreateOptionsMenuListener.call(menu);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Logger.log("ActivityListener", "onOptionsItemSelected");
		for (YailProcedure onOptionsItemSelectedListener : onOptionsItemSelectedListeners) {
			if ((boolean) onOptionsItemSelectedListener.call()) {
				return true;
			}
		}
		return false;
	}
}